package me.firstapp.manage.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hyperic.sigar.SigarException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.ServerInfo;
import me.firstapp.common.module.system.JsonStatistics;
import me.firstapp.common.utils.ServerUtils;
import me.firstapp.spi.system.StatisticsService;

@Controller
public class ManagePageController extends BaseManage {

	@Autowired
	private StatisticsService statisticsService;

	@RequestMapping(value = "/index.html", method = RequestMethod.GET)
	public ModelAndView indexView(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		return new ModelAndView("common/index", modelMap);
	}

	@RequestMapping(value = "/welcome.html", method = RequestMethod.GET)
	public ModelAndView welcomeView(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		SingleObject<JsonStatistics> result = statisticsService.findStatistics();
		modelMap.put("statistics", result.getObject());
		try {
			ServerInfo serverInfo = new ServerInfo();
			ServerUtils.cpu(serverInfo);
			ServerUtils.memory(serverInfo);
			ServerUtils.os(serverInfo);
			ServerUtils.property(serverInfo);
			serverInfo.setIp("192.168.191.1");
			serverInfo.setPort("8080");
			serverInfo.setDomain("www.firstapp.me");
			modelMap.put("serverInfo", serverInfo);
		} catch (SigarException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ModelAndView("common/welcome", modelMap);
	}
}
